package xyz.csmcool.imroom.mapper;

import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import xyz.csmcool.imroom.entity.Rooms;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import xyz.csmcool.imroom.entity.vo.RoomSimpleVo;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author csmcool
 * @since 2022-08-06
 */
@Mapper
public interface RoomsMapper extends BaseMapper<Rooms> {


    /**
     * 查找我的时间
     * @param page
     * @param limit
     * @param id
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms WHERE created_by = #{id} ) total from sjim_rooms " +
            "WHERE created_by = #{id} ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<Rooms> selectMyRooms(@Param("page") Long page,@Param("limit") Long limit,@Param("id") Integer id);

    /**
     * 查找所有的房间
     * @param page
     * @param limit
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms) total from sjim_rooms " +
            "ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<Rooms> selectAllRoom(@Param("page") Long page,@Param("limit") Long limit);


    /**
     * 搜索房间
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms WHERE `name` LIKE concat('%',#{name},'%') ) total from sjim_rooms " +
            "WHERE `name` LIKE concat('%',#{name},'%') AND created_by = #{id} ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<Rooms> searchRoom(@Param("page") Long page,@Param("limit") Long limit,
                           @Param("name") String name,@Param("id") Integer id);


    /**
     * 删除房间
     * @param roomId
     * @param createdBy
     * @return
     */
    @Delete("DELETE a,b from sjim_rooms a LEFT JOIN sjim_rooms_user b ON a.id = b.sjim_room " +
            "WHERE a.id = #{id} AND a.created_by = #{userid}")
    Boolean deleteRoom(@Param("id") Integer roomId,@Param("userid") Integer createdBy);

    /**
     * 查找所有公开的房间新的
     * @param page
     * @param limit
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms WHERE is_visible = 1) total from sjim_rooms " +
            "WHERE is_visible = 1 " +
            "ORDER BY create_time DESC LIMIT #{page},#{limit}")
    List<Rooms> selectAllPublicRoomByCreateTimeDown(@Param("page") Long page,@Param("limit") Long limit);

    /**
     * 查找所有公开的房间早的
     * @param page
     * @param limit
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms WHERE is_visible = 1) total from sjim_rooms " +
            "WHERE is_visible = 1 " +
            "ORDER BY create_time ASC LIMIT #{page},#{limit}")
    List<Rooms> selectAllPublicRoomByCreateTimeUp(@Param("page") Long page,@Param("limit") Long limit);

    /**
     * 查找所有公开的房间人少的
     * @param page
     * @param limit
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms WHERE is_visible = 1) total from sjim_rooms " +
            "WHERE is_visible = 1 " +
            "ORDER BY count ASC LIMIT #{page},#{limit}")
    List<Rooms> selectAllPublicRoomByUserCountUp(@Param("page") Long page,@Param("limit") Long limit);

    /**
     * 查找所有公开的房间人多的
     * @param page
     * @param limit
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms WHERE is_visible = 1) total from sjim_rooms " +
            "WHERE is_visible = 1 " +
            "ORDER BY count DESC LIMIT #{page},#{limit}")
    List<Rooms> selectAllPublicRoomByUserCountDown(@Param("page") Long page,@Param("limit") Long limit);

    /**
     * 查找所有公开的房间通过名字
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @Select("SELECT *, " +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = sjim_rooms.id AND is_pass = 1) count," +
            "(SELECT COUNT(*) FROM sjim_rooms WHERE is_visible = 1) total from sjim_rooms " +
            "WHERE is_visible = 1 AND `name` LIKE concat('%',#{name},'%')" +
            "ORDER BY count DESC LIMIT #{page},#{limit}")
    List<Rooms> selectAllPublicRoomByName(@Param("page") Long page,@Param("limit") Long limit,@Param("name") String name);


    /**
     * 查找我加入的聊天室
     * @param page
     * @param limit
     * @param userId
     * @return
     */
    @Select("SELECT b.*," +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = b.id AND is_pass = 1 ) count," +
            "(SELECT COUNT(*) FROM sjim_rooms_user WHERE `user` = #{userid} AND is_pass = 1) total " +
            "FROM `sjim_rooms_user` a LEFT JOIN sjim_rooms b ON a.sjim_room = b.id " +
            "WHERE a.`user` = #{userid} AND is_pass = 1 AND b.created_by != #{userid} LIMIT #{page},#{limit}")
    List<Rooms> selectMyJoinRoom(@Param("page") Long page,@Param("limit") Long limit,@Param("userid") Integer userId);

    /**
     * 搜索我加入的聊天室
     * @param page
     * @param limit
     * @param userId
     * @param name
     * @return
     */
    @Select("SELECT b.*," +
            "(SELECT COUNT(*) from sjim_rooms_user WHERE sjim_room = b.id AND is_pass = 1 ) count," +
            "(SELECT COUNT(*) FROM sjim_rooms_user a LEFT JOIN sjim_rooms b ON a.sjim_room = b.id " +
            "WHERE `user` =1  AND is_pass = 1 AND b.`name` LIKE concat('%',#{name},'%')) total " +
            "FROM `sjim_rooms_user` a LEFT JOIN sjim_rooms b ON a.sjim_room = b.id " +
            "WHERE a.`user` = #{userid} AND is_pass = 1 AND b.`name` LIKE concat('%',#{name},'%') " +
            "LIMIT #{page},#{limit}")
    List<Rooms> searchMyJoinRoom(@Param("page") Long page,@Param("limit") Long limit,
                             @Param("userid") Integer userId,@Param("name") String name);

    /**
     * 查找房间信息*
     * @param id
     * @return
     */
    @Select("SELECT `name`,cover_image coverImage,`status` FROM `sjim_rooms` WHERE id = #{id} LIMIT 1;")
    RoomSimpleVo selectOneSimpleRoom(@Param("id") Integer id);

    /**
     * 查找一条记录
     * @param id
     * @param uid
     * @return
     */
    @Select("SELECT COUNT(*) FROM `sjim_rooms` WHERE id = #{id} AND created_by = #{uid}")
    Integer isMyRoom(@Param("id") Integer id,@Param("uid") Integer uid);

    /**
     * 查询房间密码
     * @param roomId
     * @param id
     * @return
     */
    @Select("SELECT `password`,is_protected FROM sjim_rooms WHERE id = #{roomId} AND is_visible = 1")
    Rooms selectRoomPasswd(@Param("roomId") Integer roomId,@Param("id") Integer id);
}
